package com.ita.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ita.dao.OrderDao;
import com.ita.pojo.Order;

@Repository(value = "orderDao")
public class OrderDaoImpl extends BaseDaoImpl<Order> implements OrderDao {

	@Override
	@Transactional
	public int updateState(String id, int state) {

		String jpql = "update Order o set o.state=:state where o.id=:id";

		Query query = em.createQuery(jpql);
		query.setParameter("id", id);
		query.setParameter("state", state);

		query.executeUpdate();
		
		return 1;
//		return select(id);
	}

	@Override
	public List<Order> findAll(String m_id) {
		String jpql = "from Order o where o.merchant.id =:mid";
		return em.createQuery(jpql).setParameter("mid", m_id).getResultList();
	}

	@Override
	public List<Order> findAllWithState(String m_id, int state) {
		String jpql = "from Order o where o.merchant.id =:mid and o.state =:state";
		return em.createQuery(jpql).setParameter("mid", m_id).setParameter("state", state).getResultList();
	}

	@Override
	public List<Order> findAllNoState(int state) {
		String jpql = "from Order o where o.state <>:state";
		return em.createNativeQuery(jpql).setParameter("state", state).getResultList();
	}

}
